<?php

namespace app\data\controller\api;

use app\data\model\DataNewsItem;
use app\data\model\DataNewsMark;
use app\data\model\DataNewsXCollect;
use app\data\service\NewsService;
use think\admin\Controller;
use think\admin\model\SystemConfig;

/**
 * 文章接口控制器
 * Class News
 * @package app\data\controller\api
 */
class News extends Controller
{
    /**
     * 获取文章标签列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMark()
    {
        $query = DataNewsMark::mQuery()->like('name');
        $query->where(['status' => 1, 'deleted' => 0])->withoutField('sort,status,deleted');
        $this->success('获取文章标签', $query->order('sort desc,id desc')->page(false, false));
    }

    /**
     * 获取文章内容列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getItem()
    {
        if ($code = input('code', '')) {
            DataNewsItem::mk()->where(['code' => $code])->inc('num_read')->update([]);
            if (($uuid = input('uuid', 0)) > 0) {
                $data = ['uuid' => $uuid, 'code' => $code, 'type' => 3, 'status' => 2];
                DataNewsXCollect::mk()->where($data)->delete();
                DataNewsXCollect::mk()->insert($data);
            }
        }
        $query = DataNewsItem::mQuery()->field('id')->like('name,mark')->equal('id,code');
        $query->where(['deleted' => 0, 'status' => 1])->withoutField('sort,status,deleted');
        $result = $query->order('sort desc,id desc')->page(true, false, false, 15);
        NewsService::buildData($result['list'], input('uuid', 0));
        $this->success('获取文章内容', $result);
    }

    /**
     * 获取首页文章内容列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getItemfive()
    {
        $mark = input('mark', '');
        $result = DataNewsItem::field('id,name,code')->whereLike('mark','%'.$mark.'%')->where('status',1)->limit(5)->order('sort desc,id desc')->select()->toArray();
        $this->success('获取文章内容', $result);
    }

    /**
     * 获取申办指南
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getBiddingGuide()
    {
        $mark = input('mark', '');
        $result = DataNewsItem::field('id,name,code')->whereLike('mark','%'.$mark.'%')->limit(5)->order('sort desc,id desc')->select()->toArray();
        $data = [];
        foreach ($result as $k=>$v) {
            $data[$k]['letter'] = $this->getFirstCharter($v['name']);
            $data[$k]['data'] = $v;
        }
        $this->success('获取文章内容', $data);
    }

    /**
     * 获取字符串首字母
     * @param $str
     * @return string
     */
    function getFirstCharter($str){
        if(empty($str)){
            return '';
        }
        if(is_numeric($str[0])) {
            return $str[0];
        }

        $char = ord($str[0]);
        if($char >= ord('A') && $char <= ord('z')) {
            return strtoupper($str[0]);
        }
        $s1 = iconv('UTF-8','gb2312',$str);
        $s2 = iconv('gb2312','UTF-8',$s1);
        $s = $s2 == $str ? $s1 : $str;
        $asc = ord($s[0])*256+ord($s[1])-65536;
        if($asc>=-20319&&$asc<=-20284) return 'A';
        if($asc>=-20283&&$asc<=-19776) return 'B';
        if($asc>=-19775&&$asc<=-19219) return 'C';
        if($asc>=-19218&&$asc<=-18711) return 'D';
        if($asc>=-18710&&$asc<=-18527) return 'E';
        if($asc>=-18526&&$asc<=-18240) return 'F';
        if($asc>=-18239&&$asc<=-17923) return 'G';
        if($asc>=-17922&&$asc<=-17418) return 'H';
        if($asc>=-17417&&$asc<=-16475) return 'J';
        if($asc>=-16474&&$asc<=-16213) return 'K';
        if($asc>=-16212&&$asc<=-15641) return 'L';
        if($asc>=-15640&&$asc<=-15166) return 'M';
        if($asc>=-15165&&$asc<=-14923) return 'N';
        if($asc>=-14922&&$asc<=-14915) return 'O';
        if($asc>=-14914&&$asc<=-14631) return 'P';
        if($asc>=-14630&&$asc<=-14150) return 'Q';
        if($asc>=-14149&&$asc<=-14091) return 'R';
        if($asc>=-14090&&$asc<=-13319) return 'S';
        if($asc>=-13318&&$asc<=-12839) return 'T';
        if($asc>=-12838&&$asc<=-12557) return 'W';
        if($asc>=-12556&&$asc<=-11848) return 'X';
        if($asc>=-11847&&$asc<=-11056) return 'Y';
        if($asc>=-11055&&$asc<=-10247) return 'Z';
        return '';
    }

    /**
     * 获取文章评论
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getComment()
    {
        $map = $this->_vali(['code.require' => '文章不能为空！']);
        $query = DataNewsXCollect::mQuery()->where(['type' => 4, 'status' => 2]);
        $result = $query->where($map)->order('id desc')->page(true, false, false, 15);
        NewsService::buildListByUidAndCode($result['list']);
        $this->success('获取评论成功', $result);
    }

    function getconfig(){
    	$type = input('type');
    	if ($type == 0){
			$data = htmlspecialchars_decode(sysconf('config.aboutus'));
	    }else if ($type == 1){
			$data = htmlspecialchars_decode(sysconf('config.agreement'));
	    }else{
    		$data = htmlspecialchars_decode(sysconf('config.privacy'));
	    }
    	$this->success('获取成功', $data);
    }
}